//
//  BHBaseViewController.m
//  movie
//
//  Created by 白浩洋 on 2018/3/13.
//  Copyright © 2018年 白浩洋. All rights reserved.
//

#import "BHBaseViewController.h"

@interface BHBaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation BHBaseViewController

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    //======键盘事件处理=====
    //    //1.开启键盘管理功能
    //    [IQKeyboardManager sharedManager].enable = _isUseKeyboardManager;
    //    //2.打开点击空白收回键盘
    //    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = _isUseKeyboardManager;
    //    //3.是否显示键盘上方工具条(默认是开启状态)
    //    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addCustomTitleWithTitle:self.title];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F6F9"];
    [self RemoveNavigationBottomLine];
    self.isUseKeyboardManager = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:BH_Color_Main];
    self.navigationController.navigationBar.translucent = NO;
    [[UINavigationBar appearance] setTranslucent:NO];
    [self setUpNavigationBottonLine];
    if (self.navigationItem.leftBarButtonItem == nil && [self.navigationController.viewControllers count] > 1) {
        [self addCustomBackBarButtonItemWithTarget:nil action:nil];
    }
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

- (void)setIsUseKeyboardManager:(BOOL)isUseKeyboardManager
{
    _isUseKeyboardManager = isUseKeyboardManager;
    //======键盘事件处理=====
    //1.开启键盘管理功能
    //    [IQKeyboardManager sharedManager].enable = isUseKeyboardManager;
    //    //2.打开点击空白收回键盘
    //    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = isUseKeyboardManager;
    //    //3.是否显示键盘上方工具条(默认是开启状态)
    //    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}

- (void)setUpNavigationBottonLine {
    
        UINavigationBar *navigationBar = self.navigationController.navigationBar;
        [navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; // 此处使底部线条颜色为红色
        [navigationBar setShadowImage:[UIImage imageWithColor:[UIColor colorWithHexString:BH_Color_Main]]];
    
}

#pragma mark ---------- 定制返回 ----------
- (void)addCustomBackBarButtonItemWithTarget:(id)target action:(SEL)action {
    
    if (!target) {
        target = self;
        action = @selector(cancelAction:);
    }
    self.navigationItem.leftBarButtonItem = [self buttonWithImage:[[UIImage imageNamed:@"backWhite"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                 highlightedImage:[[UIImage imageNamed:@"backWhite"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]
                                                           target:target
                                                           action:action];
    
}

- (void)addCustomWhiteBackBarButtonItemWithTarget:(id)target action:(SEL)action {
    
    if (!target) {
        target = self;
        action = @selector(cancelAction:);
    }
    self.navigationItem.leftBarButtonItem = [self buttonWithImage:[[UIImage imageNamed:@"navbar_return_my"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                 highlightedImage:[[UIImage imageNamed:@"navbar_return_my"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]
                                                           target:target
                                                           action:action];
    
}

- (void)cancelAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (UIBarButtonItem *)buttonWithImage:(UIImage *)image
                    highlightedImage:(UIImage *)highlightedImage
                              target:(id)target
                              action:(SEL)action {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, 44, 44)];
    [button setImage:image forState:UIControlStateNormal];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, - PXChange(20), 0, PXChange(20))];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
    
}

#pragma mark ---------- BarButtonItem ----------
- (void)setupLeftImage:(UIImage *)image action:(SEL)action {
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:action];
    
}

- (void)setupRightImage:(UIImage *)image action:(SEL)action {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:action];
    
}

- (void)setupRightTitle:(NSString *)title action:(SEL)action {
    
    //    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleDone target:self action:action];
    CGSize Size = [title sizeWithAttributes:@{NSFontAttributeName:kFont_MainWithSize(PXChange(28))}];
    CGFloat width = Size.width + PXChange(10);
    UIButton *rBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
    [rBtn setTitle:title forState:UIControlStateNormal];
    rBtn.titleLabel.font = kFont_MainWithSize(PXChange(28));
    [rBtn setTitleColor:[UIColor colorWithHexString:BH_Color_Main] forState:UIControlStateNormal];
    [rBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rBtn];
}

- (void)setupLeftTitle:(NSString *)title action:(SEL)action {
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStyleDone target:self action:action];
    
}

#pragma mark ---------- 定制标题 ----------
- (void)addCustomTitleWithTitle:(NSString *)titleName {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor  colorWithHexString:@"#ffffff"];
    titleLabel.shadowOffset = CGSizeMake(0, 1);
    titleLabel.text = titleName;
    titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18];
    [titleLabel sizeToFit];
    [self.navigationItem setTitleView:titleLabel];
}

#pragma mark ---------- 设置导航栏的样式 ----------
- (void)setNavigationBarStyle {
    
}

- (void)backUp {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    if (self.navigationController.viewControllers.count == 1)//关闭主界面的右滑返回
    {
        return NO;
    } else {
        return YES;
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
    
}

- (void)viewAdjustmentTop:(UIScrollView*)sView {
    
    if (@available(iOS 11.0, *)) {
        if ([sView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)])
            sView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        [self viewAdjustment];
    }
}
- (void)viewAdjustment {
    
    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)])
        self.automaticallyAdjustsScrollViewInsets = NO;
    
}
- (void)RemoveNavigationBottomLine{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:BH_Color_Main] size:CGSizeMake(ScreenWidth, PXChange(2))] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

/**
 *  注销通知
 */
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

@end
